package grpc

import (
	"context"

	userv1 "gitee.com/youkelike/zvalidation/example/api/proto/gen/user/v1"

	"gitee.com/youkelike/zvalidation/example/domain"
	"gitee.com/youkelike/zvalidation/example/service"
	"google.golang.org/grpc"
)

type UserServiceServer struct {
	svc service.UserService
	userv1.UnimplementedUserServiceServer
}

func NewUserServiceServer(svc service.UserService) *UserServiceServer {
	return &UserServiceServer{
		svc: svc,
	}
}

func (c *UserServiceServer) Register(server grpc.ServiceRegistrar) {
	userv1.RegisterUserServiceServer(server, c)
}

func (c *UserServiceServer) CreateUser(ctx context.Context, request *userv1.CreateUserRequest) (*userv1.CreateUserResponse, error) {
	err := c.svc.Create(ctx, toUserDomain(request))
	return &userv1.CreateUserResponse{}, err
}

func toUserDomain(req *userv1.CreateUserRequest) domain.User {
	return domain.User{
		Username: req.Username,
		Password: req.Password,
		Email:    req.Email,
		Nickname: req.Nickname,
		Phone:    req.Phone,
	}
}
